package StudentManagementSystem;

import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

public class App {
    public static void main(String[] args) {
        ArrayList<User> list=new ArrayList<User>();
        Scanner sc=new Scanner(System.in);
       Loop: while (true) {
            System.out.println("欢迎来到学生管理系统");
            System.out.println("请选择操作：1.登录 2.注册 3.忘记密码 4.退出");
            String choose=sc.next();
            switch(choose){
                case "1"-> login(list);
                case "2"-> register(list);
                case "3"-> forgetPassword(list);
                case "4"-> {
                    exit(list);
                    System.out.println("感谢您的使用，再见");
                    break Loop; //System.exit(0);同 break Loop;
                }
                default -> System.out.println("选项错误，请重新输入");
            }
        }
    }
    private static void login(ArrayList<User> list){
        Scanner sc=new Scanner(System.in);

        for (int i = 0; i < 3; i++) {

            System.out.println("请输入用户名");
            String username = sc.next();
            //判断用户名是否存在
            boolean flag5 = contains(list, username);
            if (!flag5) {
                System.out.println("用户名" + username + "未注册，请先注册登录");
                return;
            }
            System.out.println("请输入密码");
            String password = sc.next();
            while (true) {
                String rightcode = getCode();
                System.out.println("当前正确的验证码为" + rightcode);
                System.out.println("请输入验证码");
                String code = sc.next();
                if (code.equalsIgnoreCase(rightcode)) {
                    System.out.println("验证码正确");
                    break;
                } else {
                    System.out.println("输入错误，请重新输入");
                    continue;
                }
            }

            //验证用户名是否正确
            //集合中是否包含密码和用户名
            //传递参数时，利用封装思想，传递整体
            User userInfo = new User(username, password, null, null);
            boolean result = checkUserInfo(list, userInfo);
            if (result) {
                System.out.println("登陆成功，恭喜您可以开始使用学生管理系统");
            break;
            } else {
                System.out.println("登录失败，用户名或密码错误");
            if (i==2){
                System.out.println("当前账号"+username+"被锁定，请联系客服:xxx-xxxxx");
            return;
            }else {
                System.out.println("用户名或密码错误，还剩下"+(2-i)+"次机会");
            }
            }
        }
    }

    private static boolean checkUserInfo(ArrayList<User> list,User userInfo) {
        for (int i = 0; i < list.size(); i++) {
            User user=list.get(i);
            if (user.getUsername().equals(userInfo.getUsername())&&user.getPassword().equals(userInfo.getPassword())) {
            return true;
            }
        }
        return false;
    }

    private static void register(ArrayList<User> list){
        //校验时，先校验格式，再校验是否唯一;
        System.out.println("注册用户");
        //将变量写到外面，提升变量可用的范围
        String username;
        String password;
        String personID;
        String phonenumber;
        Scanner sc;
        while (true) {
            sc = new Scanner(System.in);
            System.out.println("请输入用户名");
            username=sc.next();
            boolean flag1=checkUsername(username);
            if (!flag1){
                System.out.println("格式不满足条件，请重新输入");
                continue;
            }
            boolean falg2=contains(list,username);
            if (falg2){
                System.out.println("用户名"+username+"已存在，请重新输入");
            }else {//不存在的现象
                System.out.println("用户名"+username+"可以使用");
                break;
            }
        }
        //2.键盘录入密码
        //密码输入两次，两次一致才可以进行注册
        while (true) {
            System.out.println("请输入用户密码");
             password=sc.next();
            System.out.println("请再次验证您的密码");
            String again=sc.next();
            if (!password.equals(again)){
                System.out.println("两次密码不一致，请重新输入");
                continue;
            }else {
                System.out.println("两次密码一致，继续录入其他信息");
                break;
            }
        }
        //3.键盘录入身份证号
        //长度为18位，不可以以0开头，最后一位可以是数字，也可以是‘X’或‘x’；
        while (true) {
            System.out.println("请输入用户身份证号");
             personID=sc.next();
            boolean flag3=checkID(personID);
            if (flag3){
                System.out.println("身份证号码满足要求");
                break;
            }else {
                System.out.println("身份证号码格式错误，请重新输入");
                continue;
            }
        }
        //手机号码长度为11位，不可以以0开头，必须是纯数字。
        while (true) {
            System.out.println("请输入用户手机号");
            phonenumber=sc.next();
            boolean flag4=checkpersonPhone(phonenumber);
            if (flag4){
                System.out.println("手机号码输入正确，");
                break;
            }else {
                System.out.println("手机号码输入错误，请重新输入");
                continue;
            }
        }
        User u=new User(username,password,personID,phonenumber);
        list.add(u);
        System.out.println("恭喜您，用户注册成功");
        printList(list);
    }

    private static void printList(ArrayList<User> list) {
        for (int i = 0; i < list.size(); i++) {
            User user = list.get(i);
            System.out.println(user.getUsername()+","+user.getPassword()+","+
                    user.getPersonID()+","+ user.getPersonPhone());
        }
    }
    private static boolean checkUsername(String username){
        //长度在3~15；只可以是字母加数字的组合，不能是纯数字
        int len=username.length();
        if (len<3||len>15){
            return false;
        }
        for (int i = 0; i < username.length(); i++) {
            char c=username.charAt(i);
            if (!((c>='a'&&c<='z')||(c>='A'&&c<='Z')||(c>='0'&&c<='9'))){
                return  false;
            }
        }
        int count=0;
        for (int i = 0; i < username.length(); i++) {
            char c=username.charAt(i);
            if ((c>='a'&&c<='z')||(c>='A'&&c<='Z')){
                count++;
                break;
            }
        }
        return count>0;
    }
    private static boolean contains(ArrayList<User> list,String username){
        for (int i = 0; i < list.size(); i++) {
            User user=list.get(i);
            String rightusername=user.getUsername();
            if (rightusername.equals(username)){
                return true;
            }
        }
        return false;
    }
    private static boolean checkID(String id){
        if (id.length()!=18){
            return false;
        }
        if (id.startsWith("0")) {
            return false;
        }
        for (int i = 0; i < id.length()-1; i++) {
            char c=id.charAt(i);
            if (!(c>='0'&&c<='9')){
                return false;
            }
        }
        char endchar=id.charAt(id.length()-1);
        if ((endchar>='0'&&endchar<='9')||(endchar=='x')||(endchar=='X')){
            return true;
        }else {
            return false;
        }
    }
    private static void forgetPassword(ArrayList<User> list){
        Scanner sc=new Scanner(System.in);
        System.out.println("忘记密码");
        System.out.println("请输入用户名");
        String username=sc.next();
        boolean flag=contains(list,username);
        if (!flag){
            System.out.println("当前用户"+username+"未注册，请先注册");
        return;
        }
        System.out.println("请输入身份证号码");
        String personid=sc.next();
        System.out.println("请输入手机号码");
        String phonenumber=sc.next();
        //将用户对象通过索引获取出来
       int  index= findindex(list,username);
       User user=list.get(index);
       if (!(user.getPersonID().equalsIgnoreCase(personid)&&user.getPersonPhone().equals(phonenumber)))
       {
           System.out.println("身份证号或手机号输入有误，不能修改密码");
           return;
       }
       String password;
        while (true) {
            System.out.println("请输入新的密码");
             password=sc.next();
            System.out.println("请再次输入新的密码");
            String again=sc.next();
            if (password.equals(again)){
                System.out.println("两次密码输入一致");
                break;
            }else {
                System.out.println("两次密码输入不一致，请重新输入");
                continue;
            }
        }
        user.setPassword(password);
        System.out.println("密码修改成功");
    }

    private static int findindex(ArrayList<User> list, String username) {
        for (int i = 0; i < list.size(); i++) {
            User user=list.get(i);
            if (user.getUsername().equals(username)){
                return i;
            }
        }
        return -1;
    }

    private static void exit(ArrayList<User> list){
        System.out.println("退出");
    }
    private static boolean checkpersonPhone(String personphone){
        if (personphone.length()!=11){
            return false;
        }
        if (personphone.startsWith("0")){
            return false;
        }
        for (int i = 0; i < personphone.length(); i++) {
            char c=personphone.charAt(i);
            if (!(c>='0'&&c<='9')){
                return false;
            }
        }
        return true;
    }
    public static String getCode(){
        ArrayList<Character>list=new ArrayList<>();
        for (int i = 0; i < 26; i++) {
            list.add((char)('a'+i));
            list.add((char)('A'+i));
        }
        StringBuilder sb=new StringBuilder();
        Random r=new Random();
        for (int i = 0; i < 4; i++) {
            int index=r.nextInt(list.size());
            char c=list.get(index);
            sb.append(c);
        }
        int number=r.nextInt(10);
        sb.append(number);
        char[]arr=sb.toString().toCharArray();//先变成字符串，再成为字符数组

       int randomindex= r.nextInt(arr.length);
       char temp=arr[randomindex];
       arr[randomindex]=arr[arr.length-1];
       arr[arr.length-1]=temp;
       return new String(arr);
    }

}
